
#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include "mainwindow.h"

enum { IMG_COLUMN=0, INFO_COLUMN, N_COLUMNS };

static GtkWidget *window;
//static GtkWidget *window;
static GtkWidget *usertree;
const char *PHOTO_PATH = "../res/headerimg/";
const char *ICON_PATH = "../res/icon/";

GtkWidget * createNoteBook(NoteList *notelist);
NoteList * buildNoteList();
static void initUserTree();

/*
static void addData()
{
	GtkTreeIter toplevel,child,group;
	GtkTreeStore *treestore = GTK_TREE_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(usertree)));

	if(treestore == NULL)
		g_printf("treestore==NULL\n**************************");
	GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file("../res/headerimg/1.gif", NULL);
	pixbuf = gdk_pixbuf_scale_simple(pixbuf, 25,25,GDK_INTERP_NEAREST);
	gtk_tree_store_append(treestore, &group, NULL);
	gtk_tree_store_set(treestore, &group, INFO_COLUMN, "我的好友",-1);
	gtk_tree_store_append(treestore, &child, &group);
	gtk_tree_store_set(treestore, &child, IMG_COLUMN,pixbuf, INFO_COLUMN, "小麦",-1);
	gtk_tree_store_append(treestore, &child, &group);
	gtk_tree_store_set(treestore, &child, IMG_COLUMN, pixbuf, INFO_COLUMN, "小明",-1);
	gtk_tree_store_append(treestore, &child, &group);
	gtk_tree_store_set(treestore, &child, IMG_COLUMN, pixbuf, INFO_COLUMN, "小红",-1);
}
*/
void on_group_activated(GtkTreeView			*tree_view,
                        GtkTreePath			*path,
                        GtkTreeViewColumn	*column,
                        gpointer			user_data)
{
	GtkTreeModel *tree_store = gtk_tree_view_get_model(tree_view);
	GtkTreeIter iter;

	if(gtk_tree_view_row_expanded(tree_view, path)) {
		if(gtk_tree_model_get_iter(tree_store, &iter, path)) {
			gchar *path = g_malloc(sizeof(ICON_PATH) + 128);
			sprintf(path, "%s%s", ICON_PATH, "arrow_triangle-right.png");
			GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(path, NULL);
			pixbuf = gdk_pixbuf_scale_simple(pixbuf, 25,25,GDK_INTERP_NEAREST);	
			gtk_tree_store_set(GTK_TREE_STORE(tree_store), &iter, IMG_COLUMN, pixbuf, -1);
			g_free(path);
		}			
		gtk_tree_view_collapse_row(tree_view, path);
	} else {
		// 展开，若没有字节点将返回FALSE
		gboolean isexpanded = gtk_tree_view_expand_row(tree_view, path, FALSE);
		if(gtk_tree_model_get_iter(tree_store, &iter, path) && isexpanded) {
			
			gchar *path = g_malloc(sizeof(ICON_PATH) + 128);
			sprintf(path, "%s%s", ICON_PATH, "arrow_triangle-down.png");
			GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(path, NULL);
			pixbuf = gdk_pixbuf_scale_simple(pixbuf, 25,25,GDK_INTERP_NEAREST);	
			gtk_tree_store_set(GTK_TREE_STORE(tree_store), &iter, IMG_COLUMN, pixbuf, -1);
			g_free(path);
		}
		
	}
}

GtkWidget * createMainWindow()
{
	GtkWidget *window;
	GtkWidget *mainvBox;
	GtkWidget *tophBox;
	GtkWidget *midvBox;
	GtkWidget *bottomhBox;
	GtkWidget *notebook;
	GdkColor color;

	window = gtk_window_new(GTK_WINDOW_TOPLEVEL);	
	g_signal_connect(G_OBJECT(window),"destroy",G_CALLBACK(gtk_main_quit),NULL);  
	gtk_window_set_title(GTK_WINDOW(window),"test");  
	//设定窗口的默认宽高  
	gtk_window_set_default_size(GTK_WINDOW(window),260,350);  
	//设定窗口的位置,让窗口总在最前端  
	gtk_window_set_position(GTK_WINDOW(window),GTK_WIN_POS_CENTER);  
	//设定容器边框的宽度  
	gtk_container_set_border_width(GTK_CONTAINER(window),2);

	mainvBox = gtk_vbox_new(FALSE,0);
	tophBox = gtk_vbox_new(FALSE,0);
	midvBox = gtk_vbox_new(FALSE,0);
	bottomhBox = gtk_vbox_new(FALSE,0);

	GtkWidget *btn = gtk_button_new_with_label("Button");
	gtk_box_pack_start(GTK_BOX(tophBox), btn, TRUE, TRUE, 0);
	gtk_container_add(GTK_CONTAINER(window), mainvBox);
	gtk_box_pack_start(GTK_BOX(mainvBox), tophBox, FALSE, FALSE, 0);
	gtk_box_pack_start(GTK_BOX(mainvBox), midvBox, TRUE, TRUE, 0);
	gtk_box_pack_start(GTK_BOX(mainvBox), bottomhBox, FALSE, FALSE, 0);
	
	initUserTree();
	NoteList *notelist = buildNoteList();
	notebook = createNoteBook(notelist);
	gtk_notebook_set_show_border(GTK_NOTEBOOK(notebook),FALSE);
	update_widget_bgcolor(notebook, "#85cbb8");
	gtk_box_pack_start(GTK_BOX(midvBox), notebook, TRUE, TRUE, 0);
	g_free(notelist);
	return window;
}



// 创建好友列表树
static void initUserTree()
{

	GtkCellRenderer *renderer;
    GtkTreeViewColumn *column;
    GtkTreeStore *treestore;

	usertree = gtk_tree_view_new();
	gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(usertree), FALSE);
	renderer = gtk_cell_renderer_pixbuf_new();
	column = gtk_tree_view_column_new_with_attributes("img",renderer, "pixbuf", IMG_COLUMN, NULL);
	gtk_tree_view_append_column(GTK_TREE_VIEW(usertree), column);

    renderer = gtk_cell_renderer_text_new();
    column = gtk_tree_view_column_new_with_attributes("info", renderer, "markup", INFO_COLUMN, NULL);
	gtk_tree_view_append_column(GTK_TREE_VIEW(usertree), column);
	treestore = gtk_tree_store_new(N_COLUMNS, GDK_TYPE_PIXBUF, G_TYPE_STRING);
	gtk_tree_view_set_model(GTK_TREE_VIEW(usertree), GTK_TREE_MODEL(treestore));
	gtk_tree_view_set_grid_lines (GTK_TREE_VIEW(usertree), GTK_TREE_VIEW_GRID_LINES_NONE);
	gtk_tree_view_set_show_expanders (GTK_TREE_VIEW(usertree), FALSE);
	g_signal_connect(usertree, "row-activated", G_CALLBACK(on_group_activated), NULL);
	g_object_unref(treestore);
	
}


void addUser(MyMsgUser *user)
{
	gchar *path = g_malloc(sizeof(PHOTO_PATH) + 16);
	gchar *markup;
	markup = g_markup_printf_escaped ("<span foreground='blue' font_desc='12'>%s\n</span><span foreground='#666' font_desc='10'>%s</span>", user->nick, user->signature); 
	sprintf(path, "%s%s", PHOTO_PATH, user->photo);
	GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(path, NULL);
	if(pixbuf != NULL) {
		pixbuf = gdk_pixbuf_scale_simple(pixbuf, 25,25,GDK_INTERP_NEAREST);
		GtkTreeStore *treestore = GTK_TREE_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(usertree)));
		gtk_tree_store_append(treestore, user->iter, user->pUserGroup->iter);
		gtk_tree_store_set(treestore, user->iter, \
						IMG_COLUMN, pixbuf, \
						INFO_COLUMN, markup, -1);	
	}	
	g_free(path);
	g_free(markup);
}
void addUserGroup(gpointer groupdata, gpointer data)
{
	GtkTreeIter group;
	UserGroup *usergroup = (UserGroup *)groupdata;
	gchar *path = g_malloc(sizeof(ICON_PATH) + 128);

	sprintf(path, "%s%s", ICON_PATH, "arrow_triangle-right.png");
	GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(path, NULL);
	pixbuf = gdk_pixbuf_scale_simple(pixbuf, 25,25,GDK_INTERP_NEAREST);	

	GtkTreeStore *treestore = GTK_TREE_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(usertree)));

	gtk_tree_store_append(treestore, (GtkTreeIter *)usergroup->iter, NULL);
	gtk_tree_store_set(treestore, (GtkTreeIter *)usergroup->iter, \
				IMG_COLUMN, pixbuf, \
				INFO_COLUMN, usergroup -> groupName, -1);
	g_free(path);
}
void initUserGroup(GList *grouplist)
{
	g_list_foreach(grouplist, addUserGroup, NULL);
}
// 创建选项卡需要的数据链表
NoteList *buildNoteList()
{
	NoteList *notelist = g_malloc(sizeof(NoteList));
	notelist -> total = 0;

	GList *noteitem = NULL;

	NoteLabel *notelabel = g_malloc(sizeof(NoteLabel));
	notelabel -> labStr = "好友";
	notelabel -> notewidget = usertree;

	noteitem =  g_list_append(noteitem, notelabel);
	notelist -> total += 1;
	notelist -> top = noteitem;
	notelist -> end = noteitem;

	NoteLabel *notelabel2 = g_malloc(sizeof(NoteLabel));
	notelabel2 -> labStr = "群聊";
	notelabel2 -> notewidget = gtk_tree_view_new();;
	noteitem =  g_list_append(noteitem, notelabel2);
	notelist -> total += 1;
	notelist -> end = noteitem;

	return notelist;
}
// 创建选项卡
GtkWidget * createNoteBook(NoteList *notelist)
{
	GtkWidget *notebook;
	GtkWidget *scroll;
	GtkWidget *label;
	notebook = gtk_notebook_new();
	/*向笔记本构件中插入标签页*/ 
	gint i = 0;
	GList *noteitem = notelist -> top;
	NoteLabel *notelabel;
	for(i=0; i<notelist -> total; i++) {
		notelabel = (NoteLabel *)(noteitem -> data);
		label = gtk_label_new(notelabel->labStr);
		scroll = gtk_scrolled_window_new(NULL, NULL);
		gtk_notebook_append_page(GTK_NOTEBOOK(notebook), scroll, label);
		gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scroll), notelabel -> notewidget);
		noteitem = noteitem -> next;
	}
	return notebook;
}

// 函数功能：修改控件背景色
void update_widget_bgcolor(GtkWidget *widget, const gchar *colorStr)
{
	GdkColor color;
	gdk_color_parse(colorStr,&color);
	gtk_widget_modify_bg(widget, GTK_STATE_NORMAL, &color);
}
// 函数功能：修改背景图片
void update_widget_bg(GtkWidget *widget, const gchar *img_file)  
{          
	GtkStyle *style;
	GdkPixbuf *pixbuf;
	GdkPixmap *pixmap;
	gint width, height;
	   
	pixbuf = gdk_pixbuf_new_from_file(img_file, NULL);
	if(NULL == pixbuf)
		return;
	width = gdk_pixbuf_get_width(pixbuf);
	height = gdk_pixbuf_get_height(pixbuf);

	pixmap = gdk_pixmap_new(NULL, width, height, 24);      
	gdk_pixbuf_render_pixmap_and_mask(pixbuf, &pixmap, NULL, 0);  
	style = gtk_style_copy(GTK_WIDGET (widget)->style);       
	   
	if (style->bg_pixmap[GTK_STATE_NORMAL])           
		g_object_unref(style->bg_pixmap[GTK_STATE_NORMAL]);       
	   
	style->bg_pixmap[GTK_STATE_NORMAL] = g_object_ref(pixmap);       
	style->bg_pixmap[GTK_STATE_ACTIVE] = g_object_ref(pixmap);  
	style->bg_pixmap[GTK_STATE_PRELIGHT] = g_object_ref(pixmap);  
	style->bg_pixmap[GTK_STATE_SELECTED] = g_object_ref(pixmap);  
	style->bg_pixmap[GTK_STATE_INSENSITIVE] = g_object_ref(pixmap);  
	gtk_widget_set_style(GTK_WIDGET (widget), style);  
	g_object_unref(style);  
  
}  



